<?php
namespace app\admin\model;


use think\Exception;
use think\Model;

class Gcategory extends Model
{
    protected $pk = 'cate_id';

    protected $error;
    public function getError()
    {
        return $this->error;
    }
    public static function getCateList($data=[]) //商品列表引入分类
    {

        //分类下拉框
        $cate_list=self::field(true)->select();

        //返回值
        return $cate_list;
    }
    public function add(array $data,array $allowField = [],bool $replace = false)
    {
        try{
            if(empty($data)){
                $this->error = '数据不能为空';
            }
            if(!empty($allowField)){
                $this->allowField($allowField);
            }
            $this->replace($replace)->save($data);
            $id = $this->toArray();
            return $id[$this->pk];
        }catch (\Exception $e){
            $this->error = $e->getMessage();
            return false;
        }
    }
    public static function getGcategory()
    {
        $model = self::field(true);

        $cate_list = $model->select();
        return $cate_list;

    }
    public static function getCate($id = 0, $query=[],$page = false)
    {
        $sql = self::alias('n')->field('cate_id')->whereColumn('n.parent_id','=','w.cate_id')->limit('1')->buildsql();
        return  self::alias('w')
            ->field('cate_id,cate_name,parent_id,sort_order')
            ->field("if(".$sql.",1,0) as has_next")
            ->where('w.parent_id',$id)
            ->order('sort_order')
            ->select();

    }

    public function upt($data, $where = [],$allowField = []){
        try{
            if (empty($data)){
                $this->error = "修改数据不能为空";
                return ;
            }

            if (!empty($allowField)) {
                $this->allowField($allowField);
            }
            if (!empty($where)){
                $this->setUpdateWhere($where);

                if (isset($data[$this->pk]))   unset($data[$this->pk]);
            }

            return $this->exists(true)->save($data);
        }catch (Exception $e){
            $this->error = $e->getMessage();
            return false;
        }
    }




}